package com.ruoyi.patrol.controller;

import java.util.List;

import com.ruoyi.common.enums.UserStatus;
import com.ruoyi.common.utils.SecurityUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.patrol.domain.PtDailyForm;
import com.ruoyi.patrol.service.IPtDailyFormService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 常用表单文件Controller
 *
 * @author lkm
 * @date 2023-11-17
 */
@RestController
@RequestMapping("/patrol/dailyform")
public class PtDailyFormController extends BaseController {
    @Autowired
    private IPtDailyFormService ptDailyFormService;

    /**
     * 查询常用表单文件列表
     */
    @PreAuthorize("@ss.hasPermi('patrol:dailyform:list')")
    @GetMapping("/list")
    public TableDataInfo list(PtDailyForm ptDailyForm) {
        startPage();
        List<PtDailyForm> list = ptDailyFormService.selectPtDailyFormList(ptDailyForm);
        return getDataTable(list);
    }

    /**
     * 查询每日校园安全检查、整改记录表 表单文件数量
     */
    @PreAuthorize("@ss.hasPermi('patrol:dailyform:list')")
    @GetMapping("/thatday")
    public int thatday() {
        List<PtDailyForm> list = ptDailyFormService.selectPtThatDayFormList();
        return list.size();
    }

    /**
     * 导出常用表单文件列表
     */
    @PreAuthorize("@ss.hasPermi('patrol:dailyform:export')")
    @Log(title = "常用表单文件", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(PtDailyForm ptDailyForm) {
        List<PtDailyForm> list = ptDailyFormService.selectPtDailyFormList(ptDailyForm);
        ExcelUtil<PtDailyForm> util = new ExcelUtil<PtDailyForm>(PtDailyForm.class);
        return util.exportExcel(list, "dailyform");
    }

    /**
     * 获取常用表单文件详细信息
     */
    @PreAuthorize("@ss.hasPermi('patrol:dailyform:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return AjaxResult.success(ptDailyFormService.selectPtDailyFormById(id));
    }

    /**
     * 获取当天没有填的学校
     */
    @PreAuthorize("@ss.hasPermi('patrol:dailyform:list')")
    @GetMapping(value = "/notfilledin")
    public AjaxResult getNotFilledInList(PtDailyForm ptDailyForm) {
        return AjaxResult.success(ptDailyFormService.selectPtThatDayFormNotFilledInList(ptDailyForm));
    }


    /**
     * 新增常用表单文件
     */
    @PreAuthorize("@ss.hasPermi('patrol:dailyform:add')")
    @Log(title = "常用表单文件", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody PtDailyForm ptDailyForm) {
        Long schoolId = SecurityUtils.getSchoolId();
        if (schoolId == null) {
            return AjaxResult.error("你的账号没有绑定学校，无法上传");
        }
        return toAjax(ptDailyFormService.insertPtDailyForm(ptDailyForm));
    }

    /**
     * 修改常用表单文件
     */
    @PreAuthorize("@ss.hasPermi('patrol:dailyform:edit')")
    @Log(title = "常用表单文件", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody PtDailyForm ptDailyForm) {
        return toAjax(ptDailyFormService.updatePtDailyForm(ptDailyForm));
    }

    /**
     * 删除常用表单文件
     */
    @PreAuthorize("@ss.hasPermi('patrol:dailyform:remove')")
    @Log(title = "常用表单文件", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(ptDailyFormService.deletePtDailyFormByIds(ids));
    }
}
